<template>
  <div>
    <el-autocomplete
      ref="autocompleteRefs"
      placeholder="请输入内容"
      v-model="searchInput"
      class="input_search"
      v-scrollLoad="querySearchLoad"
      :fetch-suggestions="querySearch"
      @select="handleSelectSearch"
    >
      <!-- 使用 v-slot 绑定插槽内容 -->
      <!-- <template #prefix>
        <el-icon><Search /></el-icon>
      </template> -->

      <template #default="{ item }">
        <div class="content_box">
          <div class="searchInput_name">
            <svg-icon :icon-class="selectSvgIcons(item)" style="font-size: 20px; vertical-align: middle; margin-right: 6px"></svg-icon>
            <div>{{ item.realName }}{{ item.suffix ? `.${item.suffix}` : '' }}</div>
          </div>
          <div v-if="item.docContent" class="search_docContent" v-html="esSearchFunc(item.docContent)"></div>
          <div class="superiorsPath">
            <span>路径:{{ item.superiorsPath }}</span>
          </div>
        </div>
      </template>
    </el-autocomplete>

    <div class="page_tags">
      <el-tag class="tagclass" v-for="(item, index) in DefaultWordList" :key="index" @click="tagLabelClick(item)">{{ item }}</el-tag>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ApiPersearchInfo, ApiPerGetParentTreeNodeAll, ApiPerSearchInfo } from '@/api/personDoc/personDocApi';
import { defineProps, onMounted, ref, reactive } from 'vue';
const props = defineProps({
  pageType: {
    type: String,
    required: true
  }
});
const autocompleteRefs = ref();
const { proxy } = getCurrentInstance() as any;
const searchInput = ref('');
const searchStr = ref({
  page: 0,
  pageCount: '',
  list: []
} as any);

const searchlist = ref([]);

const fileTypeList = ref({
  personal: 0,
  team: 1,
  enterprise: 2
});
const searchLoading = ref(false); //用于搜索的滚动加载
const DefaultWordList = reactive(['总结', '报告', '计划', '提案', '合同', '数据']);

// es搜索出来的内容高亮方法
const esSearchFunc = (str: any) => {
  // 创建一个新的HTML元素，用来解析docContent
  let parser = new DOMParser();
  let doc = parser.parseFromString(str, 'text/html');
  // 找到所有 <em> 标签
  let emTags = doc.getElementsByTagName('em');

  // 遍历 <em> 标签并添加样式
  for (let i = 0; i < emTags.length; i++) {
    emTags[i].style.color = '#36a3f7'; // 改变文本颜色
    emTags[i].style.fontWeight = 'bold'; // 设置文字加粗
  }
  return doc.body.innerHTML;
};

// 点击标签
const tagLabelClick = (v: any) => {
  searchInput.value = v;
  autocompleteRefs.value.focus();
};

// 滚动加载
const querySearchLoad = () => {
  if (searchStr.value.page > searchStr.value.pageCount) {
    return;
  }

  if (!searchLoading.value) {
    searchLoading.value = true;

    ApiPerSearchInfo({
      type: 'team',
      fileName: searchInput.value,
      page: searchStr.value.page,
      size: 10
    }).then((res: any) => {
      const filteredResults = res.data.rows.filter((item: any) => item.type !== 'folder');
      searchLoading.value = false;
      searchStr.value.page += 1;
      autocompleteRefs.value.$data.suggestions = autocompleteRefs.value.$data.suggestions.concat(filteredResults);
      searchStr.value.pageCount = res.data.rows;
    });
  }
  console.log(333);
};

//icon图标
const selectSvgIcons = (v: any) => {
  return proxy.$yuUtils.selectSvgIcon(v);
};

const querySearch = (queryString: string, cb: (arg: any) => void) => {
  if (searchInput.value == '' && searchInput.value != undefined) {
    cb([]);
    return;
  }
  searchStr.value.page = 0;
  // 团队
  ApiPerSearchInfo({
    type: 'team',
    fileName: searchInput.value,
    page: searchStr.value.page,
    size: 10
  }).then((res: any) => {
    console.log(res, '搜索的值999999999999999');

    searchStr.value.page += 1;
    searchStr.value.pageCount = res.length;

    const filteredResults = res.data.rows

    searchlist.value = filteredResults;

    if (filteredResults.length == 0) {
      ElMessage.warning('未匹配到数据,请重新输入!');
    }

    cb(filteredResults);
  });
};

//数据源
// const querySearch = (queryString: string, cb: (arg: any) => void) => {
//   if (searchInput.value == '' && searchInput.value != undefined) {
//     cb([]);
//     return;
//   }
//   searchStr.value.page = 0;
//   //个人
//   ApiPersearchInfo({
//     title: searchInput.value,
//     moduleType: 'personal',
//     content: searchInput.value,
//     page: searchStr.value.page,
//     size: 10
//   }).then((res: any) => {
//     console.log(res, '搜索的值999999999999999');

//     searchStr.value.page += 1;
//     searchStr.value.pageCount = res.length;
//     searchStr.value.list = res;
//     if (res.length == 0) {
//       ElMessage.warning('未匹配到数据,请重新输入!');
//     }
//     // 调用 callback 返回建议列表的数据
//     cb(res);
//   });
// };

// const querySearch = (queryString: string, cb: (arg: any) => void) => {
//   if (searchInput.value == '' && searchInput.value != undefined) {
//     cb([]);
//     return;
//   }
//   searchStr.value.page = 0;
//   //个人
//   ApiPerSearchInfo({
//     type: 'personal',
//     fileName: searchInput.value,
//     page: searchStr.value.page,
//     size: 10
//   }).then((res: any) => {
//     console.log(res, '搜索的值999999999999999');

//     searchStr.value.page += 1;
//     searchStr.value.pageCount = res.length;
//     searchlist.value = res.data.rows;
//     if (res.length == 0) {
//       ElMessage.warning('未匹配到数据,请重新输入!');
//     }
//     // 调用 callback 返回建议列表的数据
//     cb(res.data.rows);
//   });
// };

//单击搜索
const handleSelectSearch = async (row: any) => {
  let params = {
    id: row.id,
    fileType: 1
  };

  const res = await ApiPerGetParentTreeNodeAll(params);
  let expandedKeys = res.data.map((item) => {
    let obj = {
      id: parseInt(item.id),
      label: item.realName
    };
    return obj;
  });

  sessionStorage.setItem('expandedKeys', JSON.stringify(expandedKeys));

  proxy.$router.push({ path: '/newUi/teamKnowdge/teamFile' ,query: { headNodeId: expandedKeys[0].id, teamId: row.teamId } });
};

onMounted(() => {
  // 访问 pageType
  console.log('传递的值:', props.pageType);
});
</script>

<style scoped lang="scss">
:deep(.input_search) {
  border: none;
  width: 100%;
}

:deep(.el-input__wrapper) {
  border: none;
  padding: 0px;
  box-shadow: none;
  border-radius: 10px;
}

:deep(.el-input__inner:hover),
:deep(.el-input__inner:focus) {
  border: 2px solid #4a9dec;
  box-shadow: 0px 0px 0px 7px rgb(74, 157, 236, 20%);
  background-color: white;
}

:deep(.el-input__prefix) {
  top: 2px;
}

:deep(.el-input-group__prepend) {
  border-top-left-radius: 20px;
  border-bottom-left-radius: 20px;
  background-color: #3d9cfb;
  border-color: #3d9cfb;
  color: #ffffff;
}

:deep(.el-input__inner) {
  // padding-top: 8px;
  // padding-bottom: 8px;
  // line-height: 40px;
  // height: 40px;
  // // border-radius: 16px;
  // border-top-right-radius: 20px;
  // border-bottom-right-radius: 20px;
  border: 2px solid transparent;
  outline: none;
  overflow: hidden;
  background-color: #ffffff;
  border-radius: 10px;
  transition: all 0.5s;
  // border: 2px solid #4a9dec;
  box-shadow: 0 0.4rem #dfd9d9;
  padding-left: 8px;
}

.searchInput_name {
  width: 100%;
  display: flex;
  align-items: center;

  div {
    // width: 10px;
    overflow: hidden;
    /*内容超出后隐藏*/
    text-overflow: ellipsis;
    /*超出内容显示为省略号*/
    white-space: nowrap;
    /*文本不进行换行*/
  }
}

.content_box {
  border-bottom: 1px solid #efefef;
}

.search_docContent {
  font-size: 12px;
  color: #939292;
  line-height: 22px;
}

.superiorsPath {
  font-size: 12px;
  color: #d6d6d6;
  line-height: 22px;
  text-align: right;
  overflow: hidden;
  /*内容超出后隐藏*/
  text-overflow: ellipsis;
  /*超出内容显示为省略号*/
  white-space: nowrap;
  /*文本不进行换行*/
}

.folder {
  position: relative;
  animation: float 1.8s infinite ease-in-out;
  transition: transform 0.3s ease;
  margin-bottom: 6px;
}

.folder:hover {
  transform: scale(1.05);
}

.folder .top {
  background: linear-gradient(135deg, #ff9a56, #ff6f56);
  width: 60px;
  height: 15px;
  border-radius: 12px 12px 0 0;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
  position: relative;
  z-index: 2;
}

.folder .bottom {
  background: linear-gradient(135deg, #ffe563, #ffc663);
  width: 100px;
  height: 70px;
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.3);
  border-radius: 0 10px 10px 10px;
  position: relative;
  top: -10px;
}

@keyframes float {
  0% {
    transform: translateY(0px);
  }

  50% {
    transform: translateY(-12px);
  }

  100% {
    transform: translateY(0px);
  }
}

.page_tags {
  margin-top: 15px;

  .tagclass {
    margin-right: 10px;
    cursor: pointer;
  }
}
</style>
